﻿''' <summary>
''' 设备控件
''' </summary>
''' <remarks>
''' 显示设备及端口
''' </remarks>
Public Class DeviceItem
    ''' <summary>
    ''' 设备名称
    ''' </summary>
    ''' <remarks></remarks>
    Private _DevName As String

    ''' <summary>
    ''' 设备编号
    ''' </summary>
    ''' <remarks></remarks>
    Private _DeviceID As Integer = 0

    ''' <summary>
    ''' 端口列表
    ''' </summary>
    ''' <remarks></remarks>
    Private Ports As Hashtable = Nothing

    ''===============================
    Private dxt As Integer = 1         ' 界面排列时上下间隔
    Private dyt As Integer = 19        ' 界面排列时左右间隔
    Private dcolumn As Integer = 2     ' 界面显示的列数

    ''' <summary>
    ''' 设备类型
    ''' </summary>
    ''' <remarks></remarks>
    Private _DevType As DeviceTypes = DeviceTypes.Compressor

    ''' <summary>
    ''' EX板的IP
    ''' </summary>
    ''' <remarks></remarks>
    Private _BordId As Integer = 50
    ''' <summary>
    ''' 初始化设备
    ''' </summary>
    ''' <param name="devId">设备编号</param>
    ''' <remarks></remarks>
    Public Sub New(ByVal devId As Integer, ByVal bordId As Integer, ByVal devName As String, ByVal devType As DeviceTypes)
        _DeviceID = devId
        _DevName = devName
        _DevType = devType
        _BordId = bordId
        ' 此调用是 Windows 窗体设计器所必需的。
        InitializeComponent()

        ' 在 InitializeComponent() 调用之后添加任何初始化。

        Me.lblDevName.Text = _DevName
        '' 加载端口
        InitPorts()
    End Sub


    ''' <summary>
    ''' 初始化端口
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub InitPorts()
        '' 初始化hashtable
        Ports = New Hashtable()

        CreatePorts()

    End Sub


    ''' <summary>
    ''' 创建控件
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub CreatePorts()
        Dim ds As DataSet = Nothing
        Dim dr As DataRow = Nothing
        ''=============================
        '' 查询数据库

        Select Case Me._DevType
            Case DeviceTypes.AirCooler
                '' d,acId,acDefPreAlarmPort
                Dim dal As CcapAirCoolerPortItemService = New CcapAirCoolerPortItemService()
                ds = dal.GetPorts(Me._DeviceID)
                If (ds IsNot Nothing) AndAlso ds.Tables(0).Rows.Count > 0 Then
                    dr = ds.Tables(0).Rows(0)
                    CreatePort(CInt(dr(2)), "除霜故障保护")
                End If

            Case DeviceTypes.Compressor
                '' Id ,cpId ,cpLowPressAlarmPort ,cpHighPressAlarmPort ,cpOileAlarmPort ,cpChargeTAlarmPort
                Dim dal As CcapCompressorPortItemServices = New CcapCompressorPortItemServices()
                ds = dal.GetPorts(_DeviceID)
                If (ds IsNot Nothing) AndAlso ds.Tables(0).Rows.Count > 0 Then
                    dr = ds.Tables(0).Rows(0)
                    CreatePort(CInt(dr(7)), "运行状态", False, True)
                    CreatePort(CInt(dr(2)), "低压保护")
                    CreatePort(CInt(dr(3)), "高压保护")
                    CreatePort(CInt(dr(4)), "油压保护")
                    CreatePort(CInt(dr(5)), "排气温度", True)
                    CreatePort(CInt(dr(6)), "过载保护")

                End If
            Case DeviceTypes.Condenser
                ''`Id`,`ceId`,`cePressPreAlarmPort`
                Dim dal As CcapCondenserPortItemService = New CcapCondenserPortItemService()
                ds = dal.GetPorts(_DeviceID)
                If (ds IsNot Nothing) AndAlso ds.Tables(0).Rows.Count > 0 Then
                    dr = ds.Tables(0).Rows(0)
                    CreatePort(CInt(dr(2)), "冷凝压力保护")
                End If
        End Select

        ''=============================================
        '' 重新计算控件高度
        ResizeControl()
    End Sub

    ''' <summary>
    ''' 创建并添加控件
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub CreatePort(ByVal portId As Integer, ByVal portName As String, Optional ByVal isTemperature As Boolean = False, Optional ByVal isStatusPort As Boolean = False)
        If (portId <= 0) Then Exit Sub

        ''=============================================
        '' 创建控件
        Dim ps As PortStatus

        ps = New PortStatus(portId, _DeviceID, isStatusPort)

        Dim iRow As Integer = 0
        Dim iCol As Integer = 0
        Dim iCount As Integer = Ports.Count

        iRow = Math.Floor(iCount / Me.dcolumn)
        iCol = iCount Mod Me.dcolumn

        ps.Location = New Point(iCol * (Me.dxt + ps.Width), iRow * Me.dyt)
#If DEBUG Then
        System.Diagnostics.Debug.Print(" PortName:[{0}]   X:{1}   Y:{2}", ps.PortName, ps.Location.X, ps.Location.Y)
#End If
        ps.PortName = portName
        If (isTemperature) Then
            ps.PortType = PortTypes.Temperature
        End If

        ps.BorderStyle = Windows.Forms.BorderStyle.FixedSingle

        ' ''==============================================
        ' '' 添加到界面和hashtable
        pnlPorts.Controls.Add(ps)

        Try
            If (isTemperature) Then
                '' AI 端口为在原基础上加32
                Ports.Add(Me._BordId * 100 + portId + 32, ps)
            Else
                Ports.Add(Me._BordId * 100 + portId, ps)
            End If
        Catch ex As Exception

        End Try



    End Sub

    ''' <summary>
    ''' 动态设置自己的大小
    ''' </summary>
    ''' <remarks>
    ''' 主要是高度
    ''' </remarks>
    Private Sub ResizeControl()

        If Ports.Count > 0 Then
            Me.Height = (Math.Ceiling(Ports.Count / Me.dcolumn)) * Me.dyt + 20
        Else
            Me.Height = 20 + 1 + Me.lblDevName.Height
        End If

        Me.lblDevName.Top = (Me.pnlName.Height - Me.lblDevName.Height) / 2
    End Sub

    ''' <summary>
    ''' 更具端口号获取端口信息
    ''' </summary>
    ''' <param name="portid">要获取端口的端口号</param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function GetPortItem(ByVal portid As Integer) As PortStatus

        If (Ports.ContainsKey(portid)) Then
            Return Ports.Item(portid)
        Else
            Return Nothing
        End If
    End Function

    Public Function GetPorts() As Hashtable
        Return Ports

    End Function
End Class
